/*
 *  Copyright 2010 johan.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package nu.zoom.catonine.desktop;

import java.io.File;
import java.io.IOException;

import nu.zoom.swing.desktop.WorkbenchFrame;
import nu.zoom.swing.desktop.WorkbenchFrameListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Tracks the zoom.nu desktop frames opened for tailing. The main purpose is to
 * rememeber opened files for re-opening when svansprogram is started.
 * 
 * @author "Johan Maasing" &lt;johan@zoom.nu&gt;
 */
public class TailFrameTracker implements WorkbenchFrameListener {

	private final FileReopener reopener;
	private final File fileToTrack;
	private final Log log = LogFactory.getLog(getClass());

	public TailFrameTracker(final FileReopener reopener, final File fileToTrack) {
		this.reopener = reopener;
		this.fileToTrack = fileToTrack;
		try {
			reopener.remember(fileToTrack);
		} catch (IOException ex) {
			log.warn("Unable to remember file for reopening: " + fileToTrack);
		}
	}

	@Override
	public void frameClosed(WorkbenchFrame frame) {
	}

	@Override
	public void frameWillDispose(WorkbenchFrame frame) {
		log.debug("Frame disposing, forgetting file " + fileToTrack.getName());
		try {
			reopener.forget(fileToTrack);
		} catch (IOException ex) {
			log.warn("Unable to forget file for re-opening", ex);
		}
	}

	@Override
	public void frameDetached(WorkbenchFrame frame) {
	}

	@Override
	public void frameAttached(WorkbenchFrame frame) {
	}
}
